package com.wishwzp.controller.admin;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;

import com.wishwzp.entity.ArcType;
import com.wishwzp.entity.Article;
import com.wishwzp.entity.PageBean;
import com.wishwzp.service.ArcTypeService;
import com.wishwzp.service.ArticleService;
import com.wishwzp.service.impl.InitComponent;
import com.wishwzp.util.DateUtil;
import com.wishwzp.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 帖子后台管理Controller层
 * @author user
 *
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleAdminController {

	@Resource
	private ArticleService articleService;
	
	@Resource
	private ArcTypeService arctypeService;
	
	@Resource
	private InitComponent initComponent;
	
	/**
	 * 添加或者修改帖子信息
	 * @param article
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(@RequestParam("slideImageFile") MultipartFile slideImageFile, Article article,HttpServletResponse response,HttpServletRequest request)throws Exception{
		//判断前台添加的幻灯片图片是否为空
		//如果不为空的话执行
		if(!slideImageFile.isEmpty()){
			String filePath=request.getServletContext().getRealPath("/");
			String imageName=DateUtil.getCurrentDateStr()+"."+slideImageFile.getOriginalFilename().split("\\.")[1];
			slideImageFile.transferTo(new File(filePath+"static/userImages/"+imageName));
			article.setSlideImage(imageName);
		}
		int resultTotal=0; // 操作的记录条数
		article.setPublishDate(new Date());
		if(article.getId()==null){ // 添加
			resultTotal=articleService.add(article);
		}else{ // 修改
			
		}
		StringBuffer result=new StringBuffer();
		if(resultTotal>0){
			initComponent.refreshSystem(ContextLoader.getCurrentWebApplicationContext().getServletContext());
			result.append("<script language='javascript'>alert('提交成功');</script>");
		}else{
			result.append("<script language='javascript'>alert('提交失败，请联系管理员');</script>");
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 根据条件分页查询帖子信息
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Article> articleList=articleService.list(map);
		Long total=articleService.getTotal(map);
		JSONObject result=new JSONObject();
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray=JSONArray.fromObject(articleList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	
	@RequestMapping("/arctype")
	public String arctype(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map=new HashMap<String,Object>();
		//开始页为多少
		map.put("start", pageBean.getStart());
		//一共多少页
		map.put("size", pageBean.getPageSize());
		//List<Article> articleList=articleService.list(map);
		List<ArcType> arctypeList = arctypeService.list(map);
		//Long total=articleService.getTotal(map);
		Long total = arctypeService.getTotal(map);
		JSONObject result=new JSONObject();
		JsonConfig jsonConfig=new JsonConfig();
		//jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray=JSONArray.fromObject(arctypeList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
}
